<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="12" :offset="6"><div class="grid-content bg-purple">

                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                    <el-form-item label="用户名" prop="username">
                        <el-input v-model.number="ruleForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                    </el-form-item>

                    <!--验证码-->
                    <el-form-item label="验证码" prop="password">
                        <el-input type="text" v-model="ruleForm.code" autocomplete="off"></el-input>
                        <button @click="changeCode">{{newCode}}</button>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>

            </div></el-col>
        </el-row>

        <a href="https://gitee.com/oauth/authorize?client_id=503244e5c6cb286fe8f48d822bcb306404d22208380b1775121c71b9ec117aae&redirect_uri=http%3A%2F%2F127.0.0.1%3A9001%2Fcallback&response_type=code"><img src="@/assets/mayun.jpg" ></a>

        <!--提示的文案-->
       <!-- <el-alert
                title="错误提示的文案"
                type="error"
                center
                show-icon>
        </el-alert>-->
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {

            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validateName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入账号'));
                }  else {
                    callback();
                }
            };
            return {

                ruleForm: {
                    password: '123456',
                    username: 'admin',
                    code:''
                },
                newCode: ''
                ,
                rules: {
                    password: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    username: [
                        { validator: validateName, trigger: 'blur' }
                    ]
                }
            };
        },
        created() {
            this.changeCode();
        },
        methods: {
            changeCode(){
                //do...while(条件) 循环
                do
                    this.newCode= Math.floor(Math.random()*10000);
                while( this.newCode < 1000 )



            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {

                        //验证验证码的正确性进行提交,ruleForm.code=用户输入的验证码
                        if(/*this.ruleForm.code==this.newCode*/ true){
                            //发送登录的请求
                            this.axios.post("http://localhost:9001/user/login",this.ruleForm).then(resp=>{
                                //登录成功跳转到首页
                                //把用户信息往vuex中存储一份,:提交的是vuex中的Actions
                                // this.$router.push({path:"/home"})\


                                this.$store.dispatch("loginAction",resp.data.data)//这一步写在跳转之前


                                this.$router.push({path:"/home"})




                            })
                        }else{
                            this.$message.error("验证码错误啦")
                        }


                        // alert('submit!');

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>
